package com.huarui.purchaseOrder.service.provider.taskOwner;

import java.util.ArrayList;
import java.util.List;

import com.huarui.purchaseOrder.model.PurOrSaleOrder;
import com.huarui.purchaseOrder.service.provider.remark.PurOrSaleRemarkUtil;
import com.huarui.task.model.TaskOwner;
import com.huarui.task.taskEnum.WorkFlowTaskNameEnum;

public class PurOrSaleTaskOwnerUtil {
	
	public static TaskOwner updateRemark(TaskOwner taskOwner, PurOrSaleOrder orders) {
		String remark = PurOrSaleRemarkUtil.getRemark(orders);
		taskOwner.setRemark(remark);
		return taskOwner;
	}
	
	public static TaskOwner updateTableCode(TaskOwner taskOwner, PurOrSaleOrder orders) {
		String tableCode = orders.getCreatorCompanyId().equals(orders.getBuyerId()) ? orders.getCodeOfSeller() : orders.getCodeOfBuyer();
		taskOwner.setTableCode(tableCode);
		return taskOwner;
	}
	
	public static TaskOwner updateTableCodeAndRemark(TaskOwner taskOwner, String tableCode, String remark) {
		taskOwner.setTableCode(tableCode);
		taskOwner.setRemark(remark);
		return taskOwner;
	}
	
	private static TaskOwner newTaskOwner(PurOrSaleOrder orders) {
		
		TaskOwner taskOwner = new TaskOwner();
		
		taskOwner.setOwnerId(orders.getCreatorCompanyId());
		
		String remark = PurOrSaleRemarkUtil.getRemark(orders);
		taskOwner.setRemark(remark);
		
		String tableCode = null;
		if(orders.getCreatorCompanyId().equals(orders.getBuyerId())) {
			tableCode = orders.getCodeOfBuyer();
		} else {
			tableCode = orders.getCodeOfSeller();
		}
		taskOwner.setTableCode(tableCode);
		
		String taskName = null;
		String taskNameEn = null;
		if(orders.getWorkFlowId().equals(WorkFlowTaskNameEnum.WORKFLOWID_TASKNAME_NOT_ONLINE_PURCHASE_ORDER.getWorkFlowId())) {
			taskNameEn = WorkFlowTaskNameEnum.WORKFLOWID_TASKNAME_NOT_ONLINE_PURCHASE_ORDER.getTaskNameEn();
			taskName = WorkFlowTaskNameEnum.WORKFLOWID_TASKNAME_NOT_ONLINE_PURCHASE_ORDER.getTaskName();
		} else if(orders.getWorkFlowId().equals(WorkFlowTaskNameEnum.WORKFLOWID_TASKNAME_NOT_ONLINE_SALE_ORDER.getWorkFlowId())) {
			taskNameEn = WorkFlowTaskNameEnum.WORKFLOWID_TASKNAME_NOT_ONLINE_SALE_ORDER.getTaskNameEn();
			taskName = WorkFlowTaskNameEnum.WORKFLOWID_TASKNAME_NOT_ONLINE_SALE_ORDER.getTaskName();
		} else if(orders.getWorkFlowId().equals(WorkFlowTaskNameEnum.WORKFLOWID_TASKNAME_ONLINE_PURCHASE_ORDER.getWorkFlowId())) {
			taskNameEn = WorkFlowTaskNameEnum.WORKFLOWID_TASKNAME_ONLINE_PURCHASE_ORDER.getTaskNameEn();
			taskName = WorkFlowTaskNameEnum.WORKFLOWID_TASKNAME_ONLINE_PURCHASE_ORDER.getTaskName();
		} else {
			taskNameEn = WorkFlowTaskNameEnum.WORKFLOWID_TASKNAME_ONLINE_SALE_ORDER.getTaskNameEn();
			taskName = WorkFlowTaskNameEnum.WORKFLOWID_TASKNAME_ONLINE_SALE_ORDER.getTaskName();
		}
		taskOwner.setTaskName(taskName);
		taskOwner.setTaskNameEn(taskNameEn);
		
		return taskOwner;
	}
	
	private static TaskOwner newAnotherTaskOwner(PurOrSaleOrder orders) {
		TaskOwner taskOwner = new TaskOwner();
		
		if(orders.getCreatorCompanyId().equals(orders.getBuyerId())) {
			taskOwner.setOwnerId(orders.getSellerId());
		} else {
			taskOwner.setOwnerId(orders.getBuyerId());
		}
		
		String otherRemark = PurOrSaleRemarkUtil.getRemark(orders);
		taskOwner.setRemark(otherRemark);
		
//		String tableCode = null;
//		if(getOrders().getCreatorCompanyId().equals(getOrders().getBuyerId())) {
//			tableCode = getOrders().getCodeOfSeller();
//		} else {
//			tableCode = getOrders().getCodeOfBuyer();
//		}
//		taskOwner.setTableCode(tableCode);
		
		String taskName = null;
		String taskNameEn = null;
		if(orders.getWorkFlowId().equals(WorkFlowTaskNameEnum.WORKFLOWID_TASKNAME_ONLINE_PURCHASE_ORDER.getWorkFlowId())) {
			taskNameEn = WorkFlowTaskNameEnum.WORKFLOWID_TASKNAME_ONLINE_SALE_ORDER.getTaskNameEn();
			taskName = WorkFlowTaskNameEnum.WORKFLOWID_TASKNAME_ONLINE_SALE_ORDER.getTaskName();
		} else {
			taskNameEn = WorkFlowTaskNameEnum.WORKFLOWID_TASKNAME_ONLINE_PURCHASE_ORDER.getTaskNameEn();
			taskName = WorkFlowTaskNameEnum.WORKFLOWID_TASKNAME_ONLINE_PURCHASE_ORDER.getTaskName();
		}
		taskOwner.setTaskName(taskName);
		taskOwner.setTaskNameEn(taskNameEn);
		
		return taskOwner;
	}
	
	public static Object submitTaskOwner(PurOrSaleOrder orders) {
		
		if(orders.getWorkFlowId().equals(WorkFlowTaskNameEnum.WORKFLOWID_TASKNAME_ONLINE_PURCHASE_ORDER.getWorkFlowId())) {
			List<TaskOwner> list = new ArrayList<TaskOwner>();
			TaskOwner owner1 = newTaskOwner(orders);
			list.add(owner1);
			
			if(orders.getBuyerId() != null && orders.getSellerId() != null) {
				TaskOwner owner2 = newAnotherTaskOwner(orders);
				list.add(owner2);
			}
			return list;
		} else {
			return newTaskOwner(orders);
		}
		
	}
	
	public static Object saveTaskOwner(PurOrSaleOrder orders) {
		return newTaskOwner(orders);
	}
	
	public static List<TaskOwner> saveAnotherTaskOwner(PurOrSaleOrder orders) {
		List<TaskOwner> list = new ArrayList<TaskOwner>();
		
		if(orders.getWorkFlowId().equals(WorkFlowTaskNameEnum.WORKFLOWID_TASKNAME_ONLINE_PURCHASE_ORDER.getWorkFlowId())) {
			if(orders.getBuyerId() != null && orders.getSellerId() != null) {
				list.add(newAnotherTaskOwner(orders));
			}
		}
		
		return list;
	}
	
}
